package com.china08.yxyapi.api.manage.edu.office.innernotice;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.office.YxyNoticeInner;
import com.china08.yxyapi.entity.mg.office.YxyNoticeInner.YxyNotifictionTea;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.YxyNoticeInnerRepository;

/**
 * 教育局行政办公 内部通知 收件箱删除通知
 * 
 */
@CrossOrigin
@RestController("InnerNoticeReceiveDelController")
public class InnerNoticeReceiveDelController extends YxyApiBaseController {
    @Autowired
    private YxyNoticeInnerRepository yxyNoticeInnerRepository;

    @RequestMapping(value = "/api_yw/manage/edu/office/innerNotice/receive/delete/{id}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object precess(//
            HttpServletRequest request, //
            HttpServletResponse response, //
            @PathVariable("id") String id//
    ) {
        RespModel respModel = new RespModel();
        String schoolId = getProfile(request, response).getSchoolId();
        String currentUserId = getProfile(request, response).getId();
        List<String> idLst = Arrays.asList(id);
        List<YxyNoticeInner> yxyNoticeInnerLst = yxyNoticeInnerRepository.findByIdInAndReceiverUserIdAndSchoolId(idLst, currentUserId, schoolId);
        for (YxyNoticeInner yxyNoticeInner : yxyNoticeInnerLst) {
            for (YxyNotifictionTea yxyNotifictionTea : yxyNoticeInner.getReceiver()) {
                if (yxyNotifictionTea.getIsDelete() == 0 && yxyNotifictionTea.getUserId().equals(currentUserId)) {
                    yxyNotifictionTea.setIsDelete(-1);
                    yxyNotifictionTea.setIsRead(1);
                }
            }
        }
        // 更改为删除状态
        yxyNoticeInnerRepository.save(yxyNoticeInnerLst);
        respModel.setMessage("删除成功");
        return respModel;
    }

    public class ReqModel extends YxyApiReqModel {

    }

    protected static class RespModel extends YxyApiRespModel {

    }
}